﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class CreateForumThread : System.Web.UI.Page
    {
        string ForumId;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.CreateForumThread_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            //this.Title = Resources.Resource.CreateForumThread_Title + " - " + GND.ReturnAppTitle();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            
            if (Request.QueryString["Id"] != null)
                ForumId = Request.QueryString["Id"].ToString();
            else
                Response.Redirect("~/ForumHomePage.aspx");
            
            if (!GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumIsUserForumMember", ou.UserId, ForumId))
                Response.Redirect("~/ForumHomePage.aspx");
            
            //if (ou.IsRTL())
            //{
            //    const string crlf = "\r\n";
            //    string js = "<script type='text/javascript'>" + crlf +
            //        "{" + crlf +
            //            "<script type=text/javascript>" + crlf +

            //    "Sys.Application.add_load(function () {" + crlf +

            //        "var Threadeditor = $find(<%= Threadeditor.ClientID %>);" + crlf +
            //        "var editPanel = Threadeditor.get_editPanel();" + crlf +

            //        "editPanel._setActive_saved = editPanel._setActive;" + crlf +
            //        "editPanel._setActive = function () {" + crlf +
            //            "if (this.get_activeMode() == AjaxControlToolkit.HTMLEditor.ActiveModeType.Design) {" + crlf +
            //                "var designPanel = this.get_activePanel();" + crlf +
            //                "if (this._eAfter == null) {" + crlf +
            //                    "designPanel._doc.body.style.direction = rtl;" + crlf +
            //                "}" + crlf +
            //            "}" + crlf +
            //            "this._setActive_saved();" + crlf +
            //        "};" + crlf +
            //    "});" + crlf +
            //    "</script>";
            //    Page.RegisterStartupScript("js", js);

            //}

            //TextHelpLabel.Text = GlobalFunctions.EditorHelp();

            ForumNameLinkbutton.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumName", ForumId);
            PageNameLabel2.Text = ForumNameLinkbutton.Text;

            if (!IsPostBack)
            {
                this.MessageLabel.Text = "";
                this.MessageLabel.ForeColor = System.Drawing.Color.Green;
            }

        }
        protected void OpenThread_Click(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            String ThreadName = this.ThreadNameTextBox.Text.Trim();
            String PostText = this.Threadeditor.Text.Trim();
            if (PostText.Length > 4000 || PostText.Length < 1)
            {
                this.MessageLabel.ForeColor = System.Drawing.Color.Red;
                this.MessageLabel.Text = (PostText.Length.ToString() + " " + Resources.Resource.Error);
            }
            else //input is good
            {

                string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
                System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
                dbCommand.CommandText = "Nivdal_ForumAddNewThread";
                dbCommand.CommandType = CommandType.StoredProcedure;
                dbCommand.Connection = dbConnection;

                SqlParameter dbParam_UserId = new SqlParameter();
                dbParam_UserId.ParameterName = "@UserId";
                dbParam_UserId.Value = ou.UserId;
                dbParam_UserId.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_UserId);

                SqlParameter dbParam_ForumId = new SqlParameter();
                dbParam_ForumId.ParameterName = "@ForumId";
                dbParam_ForumId.Value = ForumId;
                dbParam_ForumId.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_ForumId);

                SqlParameter dbParam_ThreadName = new SqlParameter();
                dbParam_ThreadName.ParameterName = "@ThreadName";
                dbParam_ThreadName.Value = ThreadName;
                dbParam_ThreadName.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_ThreadName);

                SqlParameter dbParam_PostText = new SqlParameter();
                dbParam_PostText.ParameterName = "@PostText";
                dbParam_PostText.Value = GlobalFunctions.EncodeHtmlFromText(PostText);
                dbParam_PostText.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_PostText);

                SqlParameter dbParam_OriginalPostText = new SqlParameter();
                dbParam_OriginalPostText.ParameterName = "@OriginalText";
                dbParam_OriginalPostText.Value = PostText;
                dbParam_OriginalPostText.DbType = DbType.String;
                dbCommand.Parameters.Add(dbParam_OriginalPostText);
                
                

                SqlParameter dbParam_ThreadId = new SqlParameter();
                dbParam_ThreadId.ParameterName = "@ThreadId";
                dbParam_ThreadId.Value = "";
                dbParam_ThreadId.DbType = DbType.Decimal;
                dbParam_ThreadId.Direction = ParameterDirection.Output;
                dbParam_ThreadId.Size = 12;
                dbCommand.Parameters.Add(dbParam_ThreadId);

                SqlParameter dbParam_Answer = new SqlParameter();
                dbParam_Answer.ParameterName = "@Answer";
                dbParam_Answer.Value = "";
                dbParam_Answer.DbType = DbType.String;
                dbParam_Answer.Direction = ParameterDirection.Output;
                dbParam_Answer.Size = 64;
                dbCommand.Parameters.Add(dbParam_Answer);

                try
                {

                    dbConnection.Open();
                    dbCommand.ExecuteScalar();
                    if (dbParam_ThreadId.Value.ToString() != "0")
                        Response.Redirect("ThreadDetails.aspx?ThreadId=" + dbParam_ThreadId.Value.ToString() + "&Answer=" + dbParam_Answer.Value.ToString());
                    else
                        this.MessageLabel.Text = dbParam_Answer.Value.ToString();
                }
                catch (Exception SQLDBException)
                {
                    this.MessageLabel.Text = Convert.ToString(SQLDBException);
                }

                finally
                {
                    dbConnection.Close();
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            this.PreviewLabel.Text = GlobalFunctions.EncodeHtmlFromText(this.Threadeditor.Text.Trim());
        }
        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }


    }
}